package week9;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author The setting sun
 * @version 1.0
 * @title: Work5
 * @description: TODO
 * @date 2021/11/4 15:51
 */
public class Work5 {
    public static void main(String[] args) {
        new MyFrame3().init();
    }
}

class MyFrame3 {
    JFrame frame = new JFrame("随机点名");
    JPanel panel1 = new JPanel();
    JLabel label = new JLabel();
    JPanel panel2 = new JPanel();
    JButton b1 = new JButton("开始");
    JButton b2 = new JButton("停止");
    Boolean flag = false;

    public void init() {
        label.setFont(new Font("微软雅黑", Font.BOLD, 18));

        panel1.add(label);
        panel2.add(b1);
        panel2.add(b2);

        frame.add(panel1, BorderLayout.CENTER);
        frame.add(panel2, BorderLayout.SOUTH);
        frame.setVisible(true);
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                flag = true;
            }
        });

        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                flag = false;
            }
        });

        String[] str = {"陈佳扬", "戴子扬", "段志凯", "龚仙贵", "桂浩", "郭俊麟", "郭磊", "郭远登",
                "何嘉辉", "何嘉翊", "贺海旺", "洪臻", "黄浩霖", "黄敏杰", "黄志威", "蒋翔", "焦柏华", "李绍政",
                "李伟杰", "李武", "刘程", "刘青", "刘治廷", "龙杰", "龙鹏骐", "龙维康", "罗步成", "罗宇翔", "宁剑",
                "欧仁杰", "盘锋", "彭鹏", "苏星", "唐恒", "唐薪", "田闯", "王磊", "吴宝龙", "伍恒", "肖盼", "肖鑫阳",
                "谢城辉", "谢城洋", "杨棚凯", "钟树涛", "周涛", "庄星鹏", "陈文萍", "罗鑫"};

        class MyThread extends Thread {
            @Override
            public void run() {
                while (true) {
                    if (flag) {
                        int id = (int) (Math.random() * 49 + 1);
                        label.setText(id + str[id - 1]);

                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        new MyThread().start();

    }
}
